package com.controller;

import com.pojo.PageResult;
import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*控制层*/
@Controller
public class UserController {
//通过类型实现注入Bytype
    @Autowired
    private UserService userService;

        /*跳转到login.jsp页面*/
    @RequestMapping("/toLogin.action")
    public  String toLogin(){
        System.out.println("tologin方法----准备跳转到登录页面login.jsp");
    return "login";
    }

     /*跳转登陆成功页面*/
    @RequestMapping("/login.action")
    public String login(HttpServletRequest request, HttpSession session){
        String name=request.getParameter("username");
        String pass=request.getParameter("password");
        //    数据库对比
            User user=userService.login(name,pass);
            if (null!=user){
                System.out.println("登陆成功");
                return"suc";
        }else{
                System.out.println("登陆失败");
                return "fail";
            }
    }
        //    存值
//        session.setAttribute("username",name);
//        System.out.println("账户："+name+"密码："+pass);
//        System.out.println("login方法 正在登陆");
//        return "IndexSuc";
//    }

      /*跳转到toRegister.jsp页面*/
    @RequestMapping("/toRegister.action")
    public String toRegister(){
        System.out.println("toregister方法---准备跳转到注册页面register.jsp");
        return "register";
    }

    /*跳转到注册成功页*/
    @RequestMapping("/register.action")
    public String register(String username,String pwd, HttpSession session){
//        String name=request.getParameter("username");
//        String pass=request.getParameter("pwd");
//        String pass1=request.getParameter("pwd1");
//        String email=request.getParameter("email");
//        String realn=request.getParameter("realname");
//        session.setAttribute("username",name);
//        session.setAttribute("pwd",pass);
//        session.setAttribute("pwd1",pass1);
//        session.setAttribute("email",email);
//        session.setAttribute("realname",realn);
        System.out.println(username+"==="+pwd);
        User user =new User();
        user.setUsername(username);

        user.setPassword(pwd);
//        增加到数据库
        int re=userService.register(user);
        if (re>0){
            System.out.println("okk");
        return "login";
        }else {
            System.out.println("nono");
        return "register";
        }
        //        System.out.println("用户名："+name+"密码："+pass1+"邮箱："+email+"真实姓名："+realn);
      /*  System.out.println("register方法--正在跳转到注册成功页");
        return "RegSuc";    */
    }

//    @RequestMapping("/toUserList.action")
//    public String toUserList(){
//        System.out.println("---");
//        return "userList";
//    }

    @RequestMapping("/toUserList.action")
    public String toUserList(String username, HttpSession session){
        List<User> userList = userService.getList(username);
        /*for (User u:userList){
            System.out.println(u.getUsername());
        }*/
        session.setAttribute("list",userList);
        return "userList";
    }

    @RequestMapping("/detail/{id}.action")
    public String detail(HttpSession session, @PathVariable(value = "id") Integer id){
        User user=userService.selectUserById(id);
        session.setAttribute("userdetail",user);
        return "datail";
    }

//    更新
    @RequestMapping("/updateUser.action")
    public String updateUser(User user){
        userService.updateUser(user);
        /*重定向*/
        return "redirect:toUserList.action";
    }
    @RequestMapping("/deleteuser.action")
    public String deleteUser(Integer id){
        userService.deleteUserById(id);
//        forward请求转发
        return "forward:toUserList.action";
    }
    @RequestMapping("/deleteUserById.action")
//    将一些数据类型转化为json
    @ResponseBody
    public Map<String, Object> delete(Integer id){
        int re=userService.deleteUserById(id);
        Map<String,Object>maps=new HashMap<>();
        if (re>0){
            maps.put("msg","success");

        }else {
            maps.put("msg","fail");

        }
        return maps;
    }
    @RequestMapping("/toUserOrderList.action")
    public String toUserOrderLisst(Integer pageNum, Integer pageSize, Model model){
        PageResult pageResult =userService.selectUserList(pageNum,pageSize);

    }
}
